package com.baidu.service.impl;

import com.baidu.mapper.TbCategoryMapper;
import com.baidu.pojo.TbCategory;
import com.baidu.pojo.TbSpecification;
import com.baidu.utils.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.pojo.TbTypeTemplate;
import com.baidu.service.TbTypeTemplateService;
import com.baidu.mapper.TbTypeTemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TbTypeTemplateServiceImpl extends ServiceImpl<TbTypeTemplateMapper, TbTypeTemplate>
implements TbTypeTemplateService{
    @Autowired
    TbTypeTemplateMapper tbTypeTemplateMapper;

    @Autowired
    private TbCategoryMapper categoryMapper;

    @Override
    public ResultResponse findTbTypeTemplateList(TbTypeTemplate tbTypeTemplate) {
        List<TbTypeTemplate> tbTypeTemplateList = tbTypeTemplateMapper.findTbTypeTemplateList(tbTypeTemplate.getName());
        return ResultResponse.SUCCESS(tbTypeTemplateList);
    }

    @Override
    public void saveTbTypeTemplate(TbTypeTemplate tbTypeTemplate) {
        tbTypeTemplateMapper.insert(tbTypeTemplate);
    }

    @Override
    public TbTypeTemplate queryTbTypeTemplateById(Integer id) {
        return tbTypeTemplateMapper.selectById(id);
    }

    @Override
    public void updateTbTypeTemplate(TbTypeTemplate tbTypeTemplate) {
        tbTypeTemplateMapper.updateById(tbTypeTemplate);
    }

    @Override
    public ResultResponse del(List<TbTypeTemplate> ids) {
        for (TbTypeTemplate typeTemplate : ids) {
            tbTypeTemplateMapper.deleteById(typeTemplate.getId());
        }
        return ResultResponse.SUCCESS();
    }


    @Override
    public ResultResponse findList() {
        List<TbTypeTemplate> tbTypeTemplates = this.tbTypeTemplateMapper.selectList(null);
        return ResultResponse.SUCCESS(tbTypeTemplates);
    }




    @Override
    public ResultResponse getTypeTemplateByTypeId(Integer typeId) {
        QueryWrapper<TbCategory> id = new QueryWrapper<TbCategory>().eq("id", typeId).last("limit 1");
        TbCategory tbCategory = categoryMapper.selectOne(id);
        QueryWrapper<TbTypeTemplate> queryWrapper = new QueryWrapper<TbTypeTemplate>().eq("name", tbCategory.getName()).last("limit 1");
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectOne(queryWrapper);
        return ResultResponse.SUCCESS(tbTypeTemplate);
    }
}




